package com.unitd.base.frame.plugin.db.datasource.factory;

import com.unitd.base.frame.plugin.db.datasource.properties.DruidProperties;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.atomikos.jdbc.AtomikosSQLException;

import java.util.Properties;

/**
 * AtomikosDataSourceBean数据源工厂
 *
 * @author Hudan
 * @version 1.0
 * @filename AtomikosFactory.java
 * @copyright www.unitd.com
 * @date 2020-06-28
 */
public class AtomikosFactory {

    /**
     * 创建分布式事务
     *
     * @param dataSourceName  数据源名称
     * @param druidProperties Druid数据连接池连接参数
     * @return 分布式事务对象
     */
    public static AtomikosDataSourceBean create(String dataSourceName, DruidProperties druidProperties) {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        atomikosDataSourceBean.setUniqueResourceName(dataSourceName);
        atomikosDataSourceBean.setMaxPoolSize(100);
        atomikosDataSourceBean.setBorrowConnectionTimeout(60);

        Properties properties = druidProperties.createProperties();
        atomikosDataSourceBean.setXaProperties(properties);

        try {
            atomikosDataSourceBean.init();
        } catch (AtomikosSQLException e) {
            e.printStackTrace();
        }
        return atomikosDataSourceBean;
    }
}